{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f728f24e-a7c2-4ad3-a4d7-18a41bc5f1b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim\n",
    "from torch.utils.data import TensorDataset, random_split, DataLoader\n",
    "from torch.optim.lr_scheduler import CosineAnnealingWarmRestarts\n",
    "from scipy.io import loadmat\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da6cdc6a-a81a-4342-a64e-7ba5b37ed4a8",
   "metadata": {},
   "source": [
    "# 读取数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e6bd3d05-6ce7-4941-90c8-a5ea71d27129",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "dict_keys(['__header__', '__version__', '__globals__', 'Label', 'feature'])\n",
      "torch.Size([48000, 2, 16, 32])\n",
      "tensor(1.)\n",
      "tensor(0.)\n"
     ]
    }
   ],
   "source": [
    "data = loadmat('../autodl-tmp/NetworkData.mat')\n",
    "print(type(data))\n",
    "print(data.keys())\n",
    "\n",
    "feature = torch.from_numpy(data['feature']).to(torch.complex64).unsqueeze(1)\n",
    "labels = torch.from_numpy(data['Label']).to(torch.complex64).unsqueeze(1)\n",
    "\n",
    "# 归一化操作：对每个通道进行 min-max 归一化\n",
    "def min_max_normalize(tensor):\n",
    "    # 按照 (C, H, W) 维度进行归一化（通常情况下 C 是第一个维度）\n",
    "    min_val = tensor.min(dim=2, keepdim=True).values  # 对每个像素点的通道（dim=2）进行最小值操作\n",
    "    max_val = tensor.max(dim=2, keepdim=True).values  # 对每个像素点的通道（dim=2）进行最大值操作\n",
    "    return (tensor - min_val) / (max_val - min_val)\n",
    "\n",
    "# 分别处理实部和虚部\n",
    "feature_real = min_max_normalize(feature.real)\n",
    "feature_imag = min_max_normalize(feature.imag)\n",
    "labels_real = min_max_normalize(labels.real)\n",
    "labels_imag = min_max_normalize(labels.imag)\n",
    "\n",
    "# 合并实部和虚部\n",
    "feature = torch.cat([feature_real, feature_imag], dim=1)\n",
    "labels = torch.cat([labels_real, labels_imag], dim=1)\n",
    "\n",
    "print(feature.shape)\n",
    "print(torch.max(feature))\n",
    "print(torch.min(feature))\n",
    "\n",
    "dataset = TensorDataset(feature, labels)\n",
    "len_dataset = len(dataset)\n",
    "# 将长度计算结果转换为整数类型\n",
    "train_size = int(0.8 * len_dataset)\n",
    "valid_size = len_dataset - train_size\n",
    "train_data, valid_data = random_split(dataset, [train_size, valid_size])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95f44f40-dc08-4384-a008-411d762b1217",
   "metadata": {},
   "source": [
    "# 定义网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f2937880-6599-400b-aa90-9d3560e6fc88",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 编码器类\n",
    "class Encoder(nn.Module):\n",
    "    def __init__(self, C_):\n",
    "        super(Encoder, self).__init__()\n",
    "        self.conv1 = nn.Conv2d(2, 16, kernel_size=4, stride=2, padding=1)\n",
    "        self.conv2 = nn.Conv2d(16, C_, kernel_size=4, stride=2, padding=1)\n",
    "    def forward(self, X):\n",
    "        Y1 = torch.relu(self.conv1(X))\n",
    "        Y = torch.relu(self.conv2(Y1))\n",
    "        return Y\n",
    "\n",
    "# 量化与二进制转换类\n",
    "class ReshapeAndQuant(nn.Module):\n",
    "    def __init__(self, q_):\n",
    "        super(ReshapeAndQuant, self).__init__()\n",
    "        self.q = q_  # 量化位数\n",
    "    def forward(self, X, v):\n",
    "        v = X.view(X.size(0), -1) # reshape\n",
    "        Z = torch.max(v) - torch.min(v)\n",
    "        mu = Z / (2 ** self.q)\n",
    "        v_quant = mu * torch.floor(v / mu)\n",
    "        v_bit = torch.round(v_quant)\n",
    "        return v_bit\n",
    "\n",
    "# 解量化与重塑类\n",
    "class DequanAndReshape(nn.Module):\n",
    "    def __init__(self, q_):\n",
    "        super(DequanAndReshape, self).__init__()\n",
    "        self.q = q_\n",
    "    def forward(self, v_bit, C_, M_, N_, N0_, Z_):\n",
    "        mu = Z_ / (2 ** self.q)\n",
    "        v_dequant = mu * v_bit\n",
    "        batch_size_ = v_bit.size(0)\n",
    "        v_reshape = v_dequant.view((batch_size_, C_, M_ // 4, N // 4 // N0)).clone()\n",
    "        return v_reshape\n",
    "\n",
    "# 残差块类\n",
    "class Residual(nn.Module):  \n",
    "    def __init__(self, input_channels, num_channels):\n",
    "        super().__init__()\n",
    "        self.conv1 = nn.Conv2d(input_channels, num_channels, kernel_size=3, stride=1, padding=1)\n",
    "        self.conv2 = nn.Conv2d(num_channels, num_channels, kernel_size=3, stride=1, padding=1) \n",
    "    def forward(self, X):\n",
    "        Y1 = F.relu(self.conv1(X))\n",
    "        Y = F.relu(self.conv2(Y1))\n",
    "        Y = Y + X  # 替代Y += X，避免就地操作\n",
    "        return Y\n",
    "# 定义残差网络块\n",
    "def resnet_block(input_channels, num_channels, num_residuals):\n",
    "    blk = []\n",
    "    for i in range(num_residuals):\n",
    "        blk.append(Residual(num_channels, num_channels))\n",
    "    return blk\n",
    "\n",
    "# 解码器类\n",
    "class Decoder(nn.Module):\n",
    "    def __init__(self, C_, B_, N0_):\n",
    "        super(Decoder, self).__init__()\n",
    "        self.tconv1 = nn.ConvTranspose2d(C_, 16, kernel_size=4, stride=2, padding=1)\n",
    "        self.tconv2 = nn.ConvTranspose2d(16, 16, kernel_size=4, stride=2, padding=1)\n",
    "        self.resblocks = nn.Sequential(*resnet_block(16, 16, B_))\n",
    "        self.upsampling = nn.ConvTranspose2d(16, 16, kernel_size=3, stride=(1, N0_), padding=1, output_padding=(0, N0_-1))\n",
    "        self.final_conv = nn.Conv2d(16, 2, kernel_size=3, stride=1, padding=1)  \n",
    "    def forward(self, X):\n",
    "        # 2 x (TConv + ReLU) \n",
    "        Y1 = F.relu(self.tconv2(F.relu(self.tconv1(X))))\n",
    "        # B x ResBlock\n",
    "        Y2 = self.resblocks(Y1)\n",
    "        # Upsampling\n",
    "        Y3 = self.upsampling(Y2)\n",
    "        # final Conv\n",
    "        Y = self.final_conv(Y3)\n",
    "        return Y\n",
    "\n",
    "# 组合网络类\n",
    "class JDCNet(nn.Module):\n",
    "    def __init__(self, encoder, reshape_and_quant,\n",
    "                 dequan_and_reshape, decoder, C_, M_, N_, N0_):\n",
    "        super(JDCNet, self).__init__()\n",
    "        self.encoder = encoder\n",
    "        self.reshape_and_quant = reshape_and_quant\n",
    "        self.dequan_and_reshape = dequan_and_reshape\n",
    "        self.decoder = decoder\n",
    "        self.C = C_\n",
    "        self.M = M_\n",
    "        self.N = N_\n",
    "        self.N0 = N0_\n",
    "    def forward(self, X):\n",
    "        # 编码器部分\n",
    "        encoded_output = self.encoder(X)\n",
    "        v_bit = self.reshape_and_quant(encoded_output, X)\n",
    "\n",
    "        # 这里假设直接通过网络获得Z，实际中经过传播后应该有另外的方法是的BS得到Z\n",
    "        Z_ = torch.max(encoded_output) - torch.min(encoded_output)\n",
    "        v_reshape = self.dequan_and_reshape(v_bit, self.C, self.M, self.N, self.N0, Z_) \n",
    "\n",
    "        # 解码器部分\n",
    "        H_hat = self.decoder(v_reshape)\n",
    "\n",
    "        return H_hat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd548025-634b-41b9-a51e-32bb3ef30e0d",
   "metadata": {},
   "source": [
    "# 定义损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c651ebe0-c80b-4f99-9ec5-dd95b5797ade",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch.linalg as LA\n",
    "\n",
    "# 定义均方误差损失函数\n",
    "def mse_loss(predictions, targets):\n",
    "    Loss = 0.0\n",
    "    for i in range(len(predictions)):\n",
    "        H = torch.complex(targets[i,0], targets[i,1])\n",
    "        H_hat = torch.complex(predictions[i,0], predictions[i,1])\n",
    "        Loss += LA.norm((H_hat - H), ord=2) ** 2\n",
    "    return Loss\n",
    "    \n",
    "def nmse_metric(predictions, targets):\n",
    "    mse = 0.0\n",
    "    for i in range(len(predictions)):\n",
    "        H = torch.complex(targets[i,0], targets[i,1])\n",
    "        H_hat = torch.complex(predictions[i,0], predictions[i,1])\n",
    "        mse += LA.norm((H_hat - H), ord=2) ** 2\n",
    "    mse = mse / len(predictions)\n",
    "    \n",
    "    var = 0.0\n",
    "    for i in range(len(predictions)):\n",
    "        H = torch.complex(targets[i,0], targets[i,1])\n",
    "        var += LA.norm((H), ord=2) ** 2\n",
    "    var = var / len(predictions)\n",
    "    \n",
    "    return (10 * torch.log10(mse / var)) \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45e24c4d-6cb5-4d15-8653-5b1b615e1789",
   "metadata": {},
   "source": [
    "# 训练及可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "90f9c7e4-ebef-4ceb-b615-889789ba1413",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from matplotlib_inline import backend_inline\n",
    "import matplotlib.animation as animation\n",
    "from IPython import display\n",
    "\n",
    "class Animator:\n",
    "    \"\"\"For plotting data in animation.\"\"\"\n",
    "    def __init__(self, xlabel=None, ylabel=None, legend=None, xlim=None,\n",
    "                 ylim=None, xscale='linear', yscale='linear',\n",
    "                 fmts=('-', 'm--', 'g-.', 'r:'), nrows=1, ncols=1,\n",
    "                 figsize=(3.5, 2.5)):\n",
    "        \"\"\"Defined in :numref:`sec_utils`\"\"\"\n",
    "        # Incrementally plot multiple lines\n",
    "        if legend is None:\n",
    "            legend = []\n",
    "        self.use_svg_display()\n",
    "        self.fig, self.axes = plt.subplots(nrows, ncols, figsize=figsize)\n",
    "        if nrows * ncols == 1:\n",
    "            self.axes = [self.axes, ]\n",
    "        # Use a lambda function to capture arguments\n",
    "        self.config_axes = lambda: self.set_axes(\n",
    "            self.axes[0], xlabel, ylabel, xlim, ylim, xscale, yscale, legend)\n",
    "        self.X, self.Y, self.fmts = None, None, fmts\n",
    "\n",
    "    def use_svg_display(self):\n",
    "        backend_inline.set_matplotlib_formats('svg')\n",
    "\n",
    "    def set_axes(self, axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):\n",
    "        axes.set_xlabel(xlabel), axes.set_ylabel(ylabel)\n",
    "        axes.set_xscale(xscale), axes.set_yscale(yscale)\n",
    "        axes.set_xlim(xlim),     axes.set_ylim(ylim)\n",
    "        if legend:\n",
    "            axes.legend(legend)\n",
    "        axes.grid()\n",
    "        \n",
    "    def add(self, x, y):\n",
    "        # Add multiple data points into the figure\n",
    "        if not hasattr(y, \"__len__\"):\n",
    "            y = [y]\n",
    "        n = len(y)\n",
    "        if not hasattr(x, \"__len__\"):\n",
    "            x = [x] * n\n",
    "        if not self.X:\n",
    "            self.X = [[] for _ in range(n)]\n",
    "        if not self.Y:\n",
    "            self.Y = [[] for _ in range(n)]\n",
    "        for i, (a, b) in enumerate(zip(x, y)):\n",
    "            if a is not None and b is not None:\n",
    "                self.X[i].append(a)\n",
    "                self.Y[i].append(b)\n",
    "        self.axes[0].cla()\n",
    "        for x, y, fmt in zip(self.X, self.Y, self.fmts):\n",
    "            self.axes[0].plot(x, y, fmt)\n",
    "        self.config_axes()\n",
    "        display.display(self.fig)\n",
    "        display.clear_output(wait=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "12dfaaf0-42f3-498b-85ec-27b78e63d017",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 余弦预热退货-学习率调制器\n",
    "class CosineAnnealingWarmUpRestarts(torch.optim.lr_scheduler._LRScheduler):\n",
    "    def __init__(self, optimizer, T_0, T_w, eta_max, eta_min=5e-5, last_epoch=-1):\n",
    "        self.T_0 = T_0  # 最大调整周期\n",
    "        self.T_w = T_w  # 预热周期\n",
    "        self.eta_max = eta_max  # 最大学习率\n",
    "        self.eta_min = eta_min  # 最小学习率\n",
    "        super(CosineAnnealingWarmUpRestarts, self).__init__(optimizer, last_epoch)\n",
    "\n",
    "    def get_lr(self):\n",
    "        epoch = self.last_epoch\n",
    "        if epoch < self.T_w:\n",
    "            # 预热阶段：线性增加学习率\n",
    "            eta_t = self.eta_min + (self.eta_max - self.eta_min) * epoch / self.T_w\n",
    "        else:\n",
    "            # 余弦退火阶段：逐渐减小学习率\n",
    "            cos_inner = (epoch - self.T_w) / (self.T_0 - self.T_w)\n",
    "            cos_inner = torch.tensor(cos_inner, dtype=torch.float32)  # 将cos_inner转换为Tensor\n",
    "            cos_out = torch.cos(torch.pi * cos_inner) + 1\n",
    "            eta_t = self.eta_min + (self.eta_max - self.eta_min) / 2 * cos_out\n",
    "        return [eta_t for _ in self.optimizer.param_groups]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b900c6a4-7fc7-4542-b790-7b765216834f",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Accumulator:\n",
    "    def __init__(self, n):\n",
    "        self.data = [0.0] * n\n",
    "\n",
    "    def add(self, *args):\n",
    "        self.data = [a + float(b) for a, b in zip(self.data, args)]\n",
    "\n",
    "    def reset(self):\n",
    "        self.data = [0.0] * len(self.data)\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        return self.data[idx]\n",
    "        \n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim\n",
    "from torch.utils.data import DataLoader\n",
    "\n",
    "def train(net, train_loader, valid_loader, num_epochs, \n",
    "          learning_rate, weight_decay, batch_size, device):\n",
    "    net.to(device)\n",
    "    train_metrics = Accumulator(1)  # 用于累积训练损失\n",
    "    valid_metrics = Accumulator(1)  # 用于累积验证损失\n",
    "\n",
    "    # 先定义优化器\n",
    "    optimizer = optim.Adam(net.parameters(), lr=learning_rate, weight_decay=weight_decay)\n",
    "    # 然后定义调度器\n",
    "    scheduler = CosineAnnealingWarmUpRestarts(optimizer, T_0=200, T_w=30, eta_max=2e-3, eta_min=5e-5)\n",
    "    # Initialize Animator\n",
    "    animator = Animator(xlabel='epoch',\n",
    "                        xlim=[1, num_epochs],\n",
    "                        legend=['Train Loss', 'Valid Loss'])\n",
    "\n",
    "    for epoch in range(num_epochs):\n",
    "        # trainloss\n",
    "        net.train()\n",
    "        train_metrics.reset()  # 重置累积器，准备累积本轮数据\n",
    "        for X, y in train_loader:\n",
    "            X, y = X.to(device), y.to(device)\n",
    "            optimizer.zero_grad()\n",
    "            y_pred = net(X)\n",
    "            loss = mse_loss(y_pred, y)\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "            train_metrics.add(loss.item())  # 累积训练损失\n",
    "\n",
    "        # Validation loss\n",
    "        net.eval()\n",
    "        valid_metrics.reset()  # 重置累积器，准备累积本轮数据\n",
    "        with torch.no_grad():\n",
    "            for X_val, y_val in valid_loader:\n",
    "                X_val, y_val = X_val.to(device), y_val.to(device)\n",
    "                y_pred_val = net(X_val)\n",
    "                valid_loss = mse_loss(y_pred_val, y_val)\n",
    "                valid_metrics.add(valid_loss.item())  # 累积验证损失\n",
    "\n",
    "        # Update animator with the losses\n",
    "        animator.add(epoch + 1, train_metrics.data + valid_metrics.data)\n",
    "\n",
    "        # 更新学习率\n",
    "        scheduler.step()\n",
    "\n",
    "        # 打印每个epoch的损失和学习率\n",
    "        print(f\"Epoch [{epoch + 1}/{num_epochs}], train loss: {train_metrics[0] / len(train_loader):.4f}, \"\n",
    "              f\"valid loss: {valid_metrics[0] / len(valid_loader):.4f}, \"\n",
    "              f\"LR: {scheduler.get_lr()[0]:.8f}\")\n",
    "\n",
    "    # 如果是最后一个 epoch，计算 NMSE 指标\n",
    "    if epoch == num_epochs - 1:\n",
    "        # 计算训练集的 NMSE\n",
    "        net.eval()\n",
    "        total_train_nmse = 0.0\n",
    "        with torch.no_grad():\n",
    "            for X_train, y_train in train_loader:\n",
    "                X_train, y_train = X_train.to(device), y_train.to(device)\n",
    "                y_pred_train = net(X_train)\n",
    "                total_train_nmse += nmse_metric(y_pred_train, y_train).item()\n",
    "\n",
    "        avg_train_nmse = total_train_nmse / len(train_loader)\n",
    "        print(f\"Final Train NMSE: {avg_train_nmse:.4f}\")\n",
    "\n",
    "        # 计算验证集的 NMSE\n",
    "        total_valid_nmse = 0.0\n",
    "        with torch.no_grad():\n",
    "            for X_val, y_val in valid_loader:\n",
    "                X_val, y_val = X_val.to(device), y_val.to(device)\n",
    "                y_pred_val = net(X_val)\n",
    "                total_valid_nmse += nmse_metric(y_pred_val, y_val).item()\n",
    "\n",
    "        avg_valid_nmse = total_valid_nmse / len(valid_loader)\n",
    "        print(f\"Final Valid NMSE: {avg_valid_nmse:.4f}\")\n",
    "        \n",
    "    return train_metrics.data, valid_metrics.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "924387c6-090d-4f59-8651-e3aa224fe933",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch [10/10], train loss: 17556.9257, valid loss: 17318.6709, LR: 0.00070000\n",
      "Final Train NMSE: -8.8697\n",
      "Final Valid NMSE: -8.8716\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"229.425pt\" height=\"193.954688pt\" viewBox=\"0 0 229.425 193.954688\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:date>2024-12-09T15:16:13.083868</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.9.2, https://matplotlib.org/</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 193.954688 \n",
       "L 229.425 193.954688 \n",
       "L 229.425 0 \n",
       "L 0 0 \n",
       "z\n",
       "\" style=\"fill: #ffffff\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 20.5625 156.398438 \n",
       "L 215.8625 156.398438 \n",
       "L 215.8625 17.798438 \n",
       "L 20.5625 17.798438 \n",
       "z\n",
       "\" style=\"fill: #ffffff\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <path d=\"M 42.2625 156.398438 \n",
       "L 42.2625 17.798438 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_2\">\n",
       "      <defs>\n",
       "       <path id=\"m14979be521\" d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m14979be521\" x=\"42.2625\" y=\"156.398438\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <!-- 2 -->\n",
       "      <g transform=\"translate(39.08125 170.996875) scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-32\" d=\"M 1228 531 \n",
       "L 3431 531 \n",
       "L 3431 0 \n",
       "L 469 0 \n",
       "L 469 531 \n",
       "Q 828 903 1448 1529 \n",
       "Q 2069 2156 2228 2338 \n",
       "Q 2531 2678 2651 2914 \n",
       "Q 2772 3150 2772 3378 \n",
       "Q 2772 3750 2511 3984 \n",
       "Q 2250 4219 1831 4219 \n",
       "Q 1534 4219 1204 4116 \n",
       "Q 875 4013 500 3803 \n",
       "L 500 4441 \n",
       "Q 881 4594 1212 4672 \n",
       "Q 1544 4750 1819 4750 \n",
       "Q 2544 4750 2975 4387 \n",
       "Q 3406 4025 3406 3419 \n",
       "Q 3406 3131 3298 2873 \n",
       "Q 3191 2616 2906 2266 \n",
       "Q 2828 2175 2409 1742 \n",
       "Q 1991 1309 1228 531 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <path d=\"M 85.6625 156.398438 \n",
       "L 85.6625 17.798438 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m14979be521\" x=\"85.6625\" y=\"156.398438\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <!-- 4 -->\n",
       "      <g transform=\"translate(82.48125 170.996875) scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \n",
       "L 825 1625 \n",
       "L 2419 1625 \n",
       "L 2419 4116 \n",
       "z\n",
       "M 2253 4666 \n",
       "L 3047 4666 \n",
       "L 3047 1625 \n",
       "L 3713 1625 \n",
       "L 3713 1100 \n",
       "L 3047 1100 \n",
       "L 3047 0 \n",
       "L 2419 0 \n",
       "L 2419 1100 \n",
       "L 313 1100 \n",
       "L 313 1709 \n",
       "L 2253 4666 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-34\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <path d=\"M 129.0625 156.398438 \n",
       "L 129.0625 17.798438 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m14979be521\" x=\"129.0625\" y=\"156.398438\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <!-- 6 -->\n",
       "      <g transform=\"translate(125.88125 170.996875) scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \n",
       "Q 1688 2584 1439 2293 \n",
       "Q 1191 2003 1191 1497 \n",
       "Q 1191 994 1439 701 \n",
       "Q 1688 409 2113 409 \n",
       "Q 2538 409 2786 701 \n",
       "Q 3034 994 3034 1497 \n",
       "Q 3034 2003 2786 2293 \n",
       "Q 2538 2584 2113 2584 \n",
       "z\n",
       "M 3366 4563 \n",
       "L 3366 3988 \n",
       "Q 3128 4100 2886 4159 \n",
       "Q 2644 4219 2406 4219 \n",
       "Q 1781 4219 1451 3797 \n",
       "Q 1122 3375 1075 2522 \n",
       "Q 1259 2794 1537 2939 \n",
       "Q 1816 3084 2150 3084 \n",
       "Q 2853 3084 3261 2657 \n",
       "Q 3669 2231 3669 1497 \n",
       "Q 3669 778 3244 343 \n",
       "Q 2819 -91 2113 -91 \n",
       "Q 1303 -91 875 529 \n",
       "Q 447 1150 447 2328 \n",
       "Q 447 3434 972 4092 \n",
       "Q 1497 4750 2381 4750 \n",
       "Q 2619 4750 2861 4703 \n",
       "Q 3103 4656 3366 4563 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-36\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <path d=\"M 172.4625 156.398438 \n",
       "L 172.4625 17.798438 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_8\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m14979be521\" x=\"172.4625\" y=\"156.398438\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <!-- 8 -->\n",
       "      <g transform=\"translate(169.28125 170.996875) scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \n",
       "Q 1584 2216 1326 1975 \n",
       "Q 1069 1734 1069 1313 \n",
       "Q 1069 891 1326 650 \n",
       "Q 1584 409 2034 409 \n",
       "Q 2484 409 2743 651 \n",
       "Q 3003 894 3003 1313 \n",
       "Q 3003 1734 2745 1975 \n",
       "Q 2488 2216 2034 2216 \n",
       "z\n",
       "M 1403 2484 \n",
       "Q 997 2584 770 2862 \n",
       "Q 544 3141 544 3541 \n",
       "Q 544 4100 942 4425 \n",
       "Q 1341 4750 2034 4750 \n",
       "Q 2731 4750 3128 4425 \n",
       "Q 3525 4100 3525 3541 \n",
       "Q 3525 3141 3298 2862 \n",
       "Q 3072 2584 2669 2484 \n",
       "Q 3125 2378 3379 2068 \n",
       "Q 3634 1759 3634 1313 \n",
       "Q 3634 634 3220 271 \n",
       "Q 2806 -91 2034 -91 \n",
       "Q 1263 -91 848 271 \n",
       "Q 434 634 434 1313 \n",
       "Q 434 1759 690 2068 \n",
       "Q 947 2378 1403 2484 \n",
       "z\n",
       "M 1172 3481 \n",
       "Q 1172 3119 1398 2916 \n",
       "Q 1625 2713 2034 2713 \n",
       "Q 2441 2713 2670 2916 \n",
       "Q 2900 3119 2900 3481 \n",
       "Q 2900 3844 2670 4047 \n",
       "Q 2441 4250 2034 4250 \n",
       "Q 1625 4250 1398 4047 \n",
       "Q 1172 3844 1172 3481 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-38\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_9\">\n",
       "      <path d=\"M 215.8625 156.398438 \n",
       "L 215.8625 17.798438 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_10\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m14979be521\" x=\"215.8625\" y=\"156.398438\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <!-- 10 -->\n",
       "      <g transform=\"translate(209.5 170.996875) scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path id=\"DejaVuSans-31\" d=\"M 794 531 \n",
       "L 1825 531 \n",
       "L 1825 4091 \n",
       "L 703 3866 \n",
       "L 703 4441 \n",
       "L 1819 4666 \n",
       "L 2450 4666 \n",
       "L 2450 531 \n",
       "L 3481 531 \n",
       "L 3481 0 \n",
       "L 794 0 \n",
       "L 794 531 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "        <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \n",
       "Q 1547 4250 1301 3770 \n",
       "Q 1056 3291 1056 2328 \n",
       "Q 1056 1369 1301 889 \n",
       "Q 1547 409 2034 409 \n",
       "Q 2525 409 2770 889 \n",
       "Q 3016 1369 3016 2328 \n",
       "Q 3016 3291 2770 3770 \n",
       "Q 2525 4250 2034 4250 \n",
       "z\n",
       "M 2034 4750 \n",
       "Q 2819 4750 3233 4129 \n",
       "Q 3647 3509 3647 2328 \n",
       "Q 3647 1150 3233 529 \n",
       "Q 2819 -91 2034 -91 \n",
       "Q 1250 -91 836 529 \n",
       "Q 422 1150 422 2328 \n",
       "Q 422 3509 836 4129 \n",
       "Q 1250 4750 2034 4750 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use xlink:href=\"#DejaVuSans-30\" x=\"63.623047\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_6\">\n",
       "     <!-- epoch -->\n",
       "     <g transform=\"translate(102.984375 184.675) scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \n",
       "L 3597 1613 \n",
       "L 953 1613 \n",
       "Q 991 1019 1311 708 \n",
       "Q 1631 397 2203 397 \n",
       "Q 2534 397 2845 478 \n",
       "Q 3156 559 3463 722 \n",
       "L 3463 178 \n",
       "Q 3153 47 2828 -22 \n",
       "Q 2503 -91 2169 -91 \n",
       "Q 1331 -91 842 396 \n",
       "Q 353 884 353 1716 \n",
       "Q 353 2575 817 3079 \n",
       "Q 1281 3584 2069 3584 \n",
       "Q 2775 3584 3186 3129 \n",
       "Q 3597 2675 3597 1894 \n",
       "z\n",
       "M 3022 2063 \n",
       "Q 3016 2534 2758 2815 \n",
       "Q 2500 3097 2075 3097 \n",
       "Q 1594 3097 1305 2825 \n",
       "Q 1016 2553 972 2059 \n",
       "L 3022 2063 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \n",
       "L 1159 -1331 \n",
       "L 581 -1331 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2969 \n",
       "Q 1341 3281 1617 3432 \n",
       "Q 1894 3584 2278 3584 \n",
       "Q 2916 3584 3314 3078 \n",
       "Q 3713 2572 3713 1747 \n",
       "Q 3713 922 3314 415 \n",
       "Q 2916 -91 2278 -91 \n",
       "Q 1894 -91 1617 61 \n",
       "Q 1341 213 1159 525 \n",
       "z\n",
       "M 3116 1747 \n",
       "Q 3116 2381 2855 2742 \n",
       "Q 2594 3103 2138 3103 \n",
       "Q 1681 3103 1420 2742 \n",
       "Q 1159 2381 1159 1747 \n",
       "Q 1159 1113 1420 752 \n",
       "Q 1681 391 2138 391 \n",
       "Q 2594 391 2855 752 \n",
       "Q 3116 1113 3116 1747 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-6f\" d=\"M 1959 3097 \n",
       "Q 1497 3097 1228 2736 \n",
       "Q 959 2375 959 1747 \n",
       "Q 959 1119 1226 758 \n",
       "Q 1494 397 1959 397 \n",
       "Q 2419 397 2687 759 \n",
       "Q 2956 1122 2956 1747 \n",
       "Q 2956 2369 2687 2733 \n",
       "Q 2419 3097 1959 3097 \n",
       "z\n",
       "M 1959 3584 \n",
       "Q 2709 3584 3137 3096 \n",
       "Q 3566 2609 3566 1747 \n",
       "Q 3566 888 3137 398 \n",
       "Q 2709 -91 1959 -91 \n",
       "Q 1206 -91 779 398 \n",
       "Q 353 888 353 1747 \n",
       "Q 353 2609 779 3096 \n",
       "Q 1206 3584 1959 3584 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \n",
       "L 3122 2828 \n",
       "Q 2878 2963 2633 3030 \n",
       "Q 2388 3097 2138 3097 \n",
       "Q 1578 3097 1268 2742 \n",
       "Q 959 2388 959 1747 \n",
       "Q 959 1106 1268 751 \n",
       "Q 1578 397 2138 397 \n",
       "Q 2388 397 2633 464 \n",
       "Q 2878 531 3122 666 \n",
       "L 3122 134 \n",
       "Q 2881 22 2623 -34 \n",
       "Q 2366 -91 2075 -91 \n",
       "Q 1284 -91 818 406 \n",
       "Q 353 903 353 1747 \n",
       "Q 353 2603 823 3093 \n",
       "Q 1294 3584 2113 3584 \n",
       "Q 2378 3584 2631 3529 \n",
       "Q 2884 3475 3122 3366 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-68\" d=\"M 3513 2113 \n",
       "L 3513 0 \n",
       "L 2938 0 \n",
       "L 2938 2094 \n",
       "Q 2938 2591 2744 2837 \n",
       "Q 2550 3084 2163 3084 \n",
       "Q 1697 3084 1428 2787 \n",
       "Q 1159 2491 1159 1978 \n",
       "L 1159 0 \n",
       "L 581 0 \n",
       "L 581 4863 \n",
       "L 1159 4863 \n",
       "L 1159 2956 \n",
       "Q 1366 3272 1645 3428 \n",
       "Q 1925 3584 2291 3584 \n",
       "Q 2894 3584 3203 3211 \n",
       "Q 3513 2838 3513 2113 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-65\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-70\" x=\"61.523438\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6f\" x=\"125\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-63\" x=\"186.181641\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-68\" x=\"241.162109\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_11\">\n",
       "      <path d=\"M 20.5625 130.941652 \n",
       "L 215.8625 130.941652 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_12\">\n",
       "      <defs>\n",
       "       <path id=\"m1fafb47887\" d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m1fafb47887\" x=\"20.5625\" y=\"130.941652\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <!-- 2 -->\n",
       "      <g transform=\"translate(7.2 134.740871) scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_13\">\n",
       "      <path d=\"M 20.5625 102.389934 \n",
       "L 215.8625 102.389934 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_14\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m1fafb47887\" x=\"20.5625\" y=\"102.389934\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <!-- 4 -->\n",
       "      <g transform=\"translate(7.2 106.189152) scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-34\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_15\">\n",
       "      <path d=\"M 20.5625 73.838215 \n",
       "L 215.8625 73.838215 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_16\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m1fafb47887\" x=\"20.5625\" y=\"73.838215\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <!-- 6 -->\n",
       "      <g transform=\"translate(7.2 77.637434) scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-36\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_17\">\n",
       "      <path d=\"M 20.5625 45.286497 \n",
       "L 215.8625 45.286497 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_18\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m1fafb47887\" x=\"20.5625\" y=\"45.286497\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_10\">\n",
       "      <!-- 8 -->\n",
       "      <g transform=\"translate(7.2 49.085716) scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-38\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_11\">\n",
       "     <!-- 1e6 -->\n",
       "     <g transform=\"translate(20.5625 14.798438) scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-65\" x=\"63.623047\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-36\" x=\"125.146484\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_19\">\n",
       "    <path d=\"M 20.5625 24.098437 \n",
       "L 42.2625 120.713869 \n",
       "L 63.9625 121.013124 \n",
       "L 85.6625 121.476842 \n",
       "L 107.3625 121.759004 \n",
       "L 129.0625 121.860623 \n",
       "L 150.7625 121.885334 \n",
       "L 172.4625 121.890893 \n",
       "L 194.1625 121.893906 \n",
       "L 215.8625 121.89734 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_20\">\n",
       "    <path d=\"M 20.5625 149.756901 \n",
       "L 42.2625 149.828598 \n",
       "L 63.9625 149.930933 \n",
       "L 85.6625 150.035272 \n",
       "L 107.3625 150.081715 \n",
       "L 129.0625 150.094144 \n",
       "L 150.7625 150.096454 \n",
       "L 172.4625 150.097855 \n",
       "L 194.1625 150.098437 \n",
       "L 215.8625 150.098292 \n",
       "\" clip-path=\"url(#p304b2aa1ae)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 20.5625 156.398438 \n",
       "L 20.5625 17.798437 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 215.8625 156.398438 \n",
       "L 215.8625 17.798437 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 20.5625 156.398438 \n",
       "L 215.8625 156.398438 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 20.5625 17.798438 \n",
       "L 215.8625 17.798438 \n",
       "\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"legend_1\">\n",
       "    <g id=\"patch_7\">\n",
       "     <path d=\"M 127.651563 55.154687 \n",
       "L 208.8625 55.154687 \n",
       "Q 210.8625 55.154687 210.8625 53.154687 \n",
       "L 210.8625 24.798438 \n",
       "Q 210.8625 22.798438 208.8625 22.798438 \n",
       "L 127.651563 22.798438 \n",
       "Q 125.651563 22.798438 125.651563 24.798438 \n",
       "L 125.651563 53.154687 \n",
       "Q 125.651563 55.154687 127.651563 55.154687 \n",
       "z\n",
       "\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n",
       "    </g>\n",
       "    <g id=\"line2d_21\">\n",
       "     <path d=\"M 129.651563 30.896875 \n",
       "L 139.651563 30.896875 \n",
       "L 149.651563 30.896875 \n",
       "\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n",
       "    </g>\n",
       "    <g id=\"text_12\">\n",
       "     <!-- Train Loss -->\n",
       "     <g transform=\"translate(157.651563 34.396875) scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path id=\"DejaVuSans-54\" d=\"M -19 4666 \n",
       "L 3928 4666 \n",
       "L 3928 4134 \n",
       "L 2272 4134 \n",
       "L 2272 0 \n",
       "L 1638 0 \n",
       "L 1638 4134 \n",
       "L -19 4134 \n",
       "L -19 4666 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-72\" d=\"M 2631 2963 \n",
       "Q 2534 3019 2420 3045 \n",
       "Q 2306 3072 2169 3072 \n",
       "Q 1681 3072 1420 2755 \n",
       "Q 1159 2438 1159 1844 \n",
       "L 1159 0 \n",
       "L 581 0 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2956 \n",
       "Q 1341 3275 1631 3429 \n",
       "Q 1922 3584 2338 3584 \n",
       "Q 2397 3584 2469 3576 \n",
       "Q 2541 3569 2628 3553 \n",
       "L 2631 2963 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-61\" d=\"M 2194 1759 \n",
       "Q 1497 1759 1228 1600 \n",
       "Q 959 1441 959 1056 \n",
       "Q 959 750 1161 570 \n",
       "Q 1363 391 1709 391 \n",
       "Q 2188 391 2477 730 \n",
       "Q 2766 1069 2766 1631 \n",
       "L 2766 1759 \n",
       "L 2194 1759 \n",
       "z\n",
       "M 3341 1997 \n",
       "L 3341 0 \n",
       "L 2766 0 \n",
       "L 2766 531 \n",
       "Q 2569 213 2275 61 \n",
       "Q 1981 -91 1556 -91 \n",
       "Q 1019 -91 701 211 \n",
       "Q 384 513 384 1019 \n",
       "Q 384 1609 779 1909 \n",
       "Q 1175 2209 1959 2209 \n",
       "L 2766 2209 \n",
       "L 2766 2266 \n",
       "Q 2766 2663 2505 2880 \n",
       "Q 2244 3097 1772 3097 \n",
       "Q 1472 3097 1187 3025 \n",
       "Q 903 2953 641 2809 \n",
       "L 641 3341 \n",
       "Q 956 3463 1253 3523 \n",
       "Q 1550 3584 1831 3584 \n",
       "Q 2591 3584 2966 3190 \n",
       "Q 3341 2797 3341 1997 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-69\" d=\"M 603 3500 \n",
       "L 1178 3500 \n",
       "L 1178 0 \n",
       "L 603 0 \n",
       "L 603 3500 \n",
       "z\n",
       "M 603 4863 \n",
       "L 1178 4863 \n",
       "L 1178 4134 \n",
       "L 603 4134 \n",
       "L 603 4863 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-6e\" d=\"M 3513 2113 \n",
       "L 3513 0 \n",
       "L 2938 0 \n",
       "L 2938 2094 \n",
       "Q 2938 2591 2744 2837 \n",
       "Q 2550 3084 2163 3084 \n",
       "Q 1697 3084 1428 2787 \n",
       "Q 1159 2491 1159 1978 \n",
       "L 1159 0 \n",
       "L 581 0 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2956 \n",
       "Q 1366 3272 1645 3428 \n",
       "Q 1925 3584 2291 3584 \n",
       "Q 2894 3584 3203 3211 \n",
       "Q 3513 2838 3513 2113 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-20\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-4c\" d=\"M 628 4666 \n",
       "L 1259 4666 \n",
       "L 1259 531 \n",
       "L 3531 531 \n",
       "L 3531 0 \n",
       "L 628 0 \n",
       "L 628 4666 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-73\" d=\"M 2834 3397 \n",
       "L 2834 2853 \n",
       "Q 2591 2978 2328 3040 \n",
       "Q 2066 3103 1784 3103 \n",
       "Q 1356 3103 1142 2972 \n",
       "Q 928 2841 928 2578 \n",
       "Q 928 2378 1081 2264 \n",
       "Q 1234 2150 1697 2047 \n",
       "L 1894 2003 \n",
       "Q 2506 1872 2764 1633 \n",
       "Q 3022 1394 3022 966 \n",
       "Q 3022 478 2636 193 \n",
       "Q 2250 -91 1575 -91 \n",
       "Q 1294 -91 989 -36 \n",
       "Q 684 19 347 128 \n",
       "L 347 722 \n",
       "Q 666 556 975 473 \n",
       "Q 1284 391 1588 391 \n",
       "Q 1994 391 2212 530 \n",
       "Q 2431 669 2431 922 \n",
       "Q 2431 1156 2273 1281 \n",
       "Q 2116 1406 1581 1522 \n",
       "L 1381 1569 \n",
       "Q 847 1681 609 1914 \n",
       "Q 372 2147 372 2553 \n",
       "Q 372 3047 722 3315 \n",
       "Q 1072 3584 1716 3584 \n",
       "Q 2034 3584 2315 3537 \n",
       "Q 2597 3491 2834 3397 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-54\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-72\" x=\"46.333984\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-61\" x=\"87.447266\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-69\" x=\"148.726562\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6e\" x=\"176.509766\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"239.888672\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-4c\" x=\"271.675781\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6f\" x=\"325.638672\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-73\" x=\"386.820312\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-73\" x=\"438.919922\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"line2d_22\">\n",
       "     <path d=\"M 129.651563 45.575 \n",
       "L 139.651563 45.575 \n",
       "L 149.651563 45.575 \n",
       "\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n",
       "    </g>\n",
       "    <g id=\"text_13\">\n",
       "     <!-- Valid Loss -->\n",
       "     <g transform=\"translate(157.651563 49.075) scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path id=\"DejaVuSans-56\" d=\"M 1831 0 \n",
       "L 50 4666 \n",
       "L 709 4666 \n",
       "L 2188 738 \n",
       "L 3669 4666 \n",
       "L 4325 4666 \n",
       "L 2547 0 \n",
       "L 1831 0 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-6c\" d=\"M 603 4863 \n",
       "L 1178 4863 \n",
       "L 1178 0 \n",
       "L 603 0 \n",
       "L 603 4863 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-64\" d=\"M 2906 2969 \n",
       "L 2906 4863 \n",
       "L 3481 4863 \n",
       "L 3481 0 \n",
       "L 2906 0 \n",
       "L 2906 525 \n",
       "Q 2725 213 2448 61 \n",
       "Q 2172 -91 1784 -91 \n",
       "Q 1150 -91 751 415 \n",
       "Q 353 922 353 1747 \n",
       "Q 353 2572 751 3078 \n",
       "Q 1150 3584 1784 3584 \n",
       "Q 2172 3584 2448 3432 \n",
       "Q 2725 3281 2906 2969 \n",
       "z\n",
       "M 947 1747 \n",
       "Q 947 1113 1208 752 \n",
       "Q 1469 391 1925 391 \n",
       "Q 2381 391 2643 752 \n",
       "Q 2906 1113 2906 1747 \n",
       "Q 2906 2381 2643 2742 \n",
       "Q 2381 3103 1925 3103 \n",
       "Q 1469 3103 1208 2742 \n",
       "Q 947 2381 947 1747 \n",
       "z\n",
       "\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-56\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-61\" x=\"60.658203\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6c\" x=\"121.9375\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-69\" x=\"149.720703\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-64\" x=\"177.503906\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-20\" x=\"240.980469\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-4c\" x=\"272.767578\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-6f\" x=\"326.730469\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-73\" x=\"387.912109\"/>\n",
       "      <use xlink:href=\"#DejaVuSans-73\" x=\"440.011719\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p304b2aa1ae\">\n",
       "   <rect x=\"20.5625\" y=\"17.798438\" width=\"195.3\" height=\"138.6\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 350x250 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Hyperparameters\n",
    "C = 8\n",
    "M = 16\n",
    "N = 64\n",
    "N0 = 2  # 2 or 4\n",
    "B = 4\n",
    "q = 8\n",
    "batch_size = 256\n",
    "num_workers = 16\n",
    "epochs = 10\n",
    "lr = 0.002\n",
    "weight_decay = 0\n",
    "# 超参数设置\n",
    "eta_max = 2e-3\n",
    "eta_min = 5e-5\n",
    "T_w = 30  # 预热周期\n",
    "T_0 = 200  # 最大调整周期\n",
    "\n",
    "\n",
    "train_loader = torch.utils.data.DataLoader(train_data, \n",
    "                batch_size=batch_size, shuffle=True, \n",
    "                num_workers=num_workers, pin_memory=True)\n",
    "valid_loader = torch.utils.data.DataLoader(valid_data, \n",
    "                batch_size=batch_size, shuffle=False, \n",
    "                num_workers=num_workers, pin_memory=True)\n",
    "\n",
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "\n",
    "# Instantiate the network\n",
    "encoder = Encoder(C_=C)\n",
    "reshape_and_quant = ReshapeAndQuant(q_=q)\n",
    "dequan_and_reshape = DequanAndReshape(q_=q)\n",
    "decoder = Decoder(C_=C, B_=B, N0_=N0)\n",
    "net = JDCNet(encoder, reshape_and_quant, dequan_and_reshape, decoder,\n",
    "            C_=C, M_=M, N_=N, N0_=N0)\n",
    "\n",
    "# 在模型实例化后使用 DataParallel，实现数据并行\n",
    "net = torch.nn.DataParallel(net)\n",
    "\n",
    "net.to(device)\n",
    "\n",
    "train_net = train(net, train_loader, valid_loader, num_epochs=epochs, learning_rate=eta_max, \n",
    "      weight_decay=0, batch_size=256, device=device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09c9346f-c032-43b6-bd6f-4397291494a4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
